package io.aio;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.channels.AsynchronousSocketChannel;
import java.util.concurrent.CountDownLatch;

/**
 *@Description:<p>客户端处理器，AIO模型</p>
 *@author 王旭
 *@time 2016年3月17日 下午11:17:29
 */
public class TimeClientHandler implements Runnable {
	
	private int port;
	
	private String host;
	
	public CountDownLatch latch;
	
	public AsynchronousSocketChannel socketChannel;
	
	public TimeClientHandler(String host, int port) {
		this.port = port;
		this.host = host;
		
		try {
			//打开通道
			this.socketChannel = AsynchronousSocketChannel.open();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	
	
	public void run() {
		
		latch = new CountDownLatch(1);
		
		this.socketChannel.connect(new InetSocketAddress(host, port), this.latch, new ConnectCompletionHandler(this.socketChannel));
		
		try {
			latch.await();
			this.socketChannel.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		
	}

}
